import request from '../../../zhou-front/src/utils/request'

export interface CartFormDTO {
  itemId: number
  num: number
  name: string
  spec: string
  price: number
  image: string
}

export interface Cart {
  id: number
  userId: number
  itemId: number
  name: string
  image: string
  price: number
  num: number
  stock: number
  createTime?: string
  updateTime?: string
}

export interface CartVO {
  id: number
  userId: number
  itemId: number
  name: string
  image: string
  price: number
  num: number
  stock: number
  spec?: string // 商品规格
}

// 添加商品到购物车
export const addItem2Cart = (data: CartFormDTO) => {
  return request({
    url: '/carts',
    method: 'post',
    data
  })
}

// 更新购物车数据
export const updateCartItem = (data: Cart) => {
  return request({
    url: '/carts',
    method: 'put',
    data
  })
}

// 删除购物车中商品
export const deleteCartItem = (id: number) => {
  return request({
    url: `/carts/${id}`,
    method: 'delete'
  })
}

// 查询购物车列表
export const queryMyCarts = () => {
  return request({
    url: '/carts',
    method: 'get'
  })
}

// 批量删除购物车中商品
export const deleteCartItemByIds = (ids: number[]) => {
  return request({
    url: '/carts',
    method: 'delete',
    params: { ids: ids.join(',') } // 假设后端接收逗号分隔的ID字符串
  })
}
